package com.cchip.cloud;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.cchip.cloud.api.MeetingApi;
import com.cchip.cloud.api.MeetingReturned;
import com.cchip.cloud.system.Account;
import com.cchip.cloud.system.BackgroundRunner;

public class RegisterActivity extends Activity {

	private EditText mUsernameEtxt = null;
	private EditText mPasswordEtxt = null;
	private EditText mRepeatPasswordEtxt = null;
	private EditText mEmailEtxt = null;
	private Button mCancelBtn = null;
	private Button mRegisterBtn = null;

	private MeetingApi mMeetingApi = MeetingApi.getInstance();
	private Requester mRequester = null;
	private Refresher mRefresher = null;

	private static final int REQ_REGISTER = 11;
	private static final int REFRESH_SHOW_REG_DIALOG = 20;
	private static final int REFRESH_LOCAL_CHECK_HINT = 21;

	private Activity context = null;
	private ProgressDialog mWaitDialog = null;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		context = this;
		setContentView(R.layout.register);

		mRequester = new Requester(BackgroundRunner.getRunner());
		mRefresher = new Refresher();

		mUsernameEtxt = (EditText) findViewById(R.id.username);
		mPasswordEtxt = (EditText) findViewById(R.id.password);
		mRepeatPasswordEtxt = (EditText) findViewById(R.id.repeat_password);
		mEmailEtxt = (EditText) findViewById(R.id.email);
		mCancelBtn = (Button) findViewById(R.id.cancel);
		mRegisterBtn = (Button) findViewById(R.id.register);

		mCancelBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				context.finish();
			}

		});

		mRegisterBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				showWaitDialog();
				mRequester.sendEmptyMessage(REQ_REGISTER);
			}

		});

	}

	private void showWaitDialog() {
		mWaitDialog = new ProgressDialog(this, ProgressDialog.STYLE_SPINNER);
		mWaitDialog.setMessage("用户注册中...");
		mWaitDialog.setCancelable(false);
		mWaitDialog.show();
	}

	private void showRegisterDialog() {
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setTitle("注册成功");
		builder.setMessage("恭喜您注册成功，系统为您分配的会议室号码为: " + Account.my.roomId);
		builder.setCancelable(false);
		builder.setNeutralButton("确定", new DialogInterface.OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {
				context.setResult(1);
				context.finish();
			}
		});
		builder.show();
	}

	private class Requester extends Handler {

		public Requester(Looper looper) {
			super(looper);
		}

		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);

			if (msg.what == REQ_REGISTER) {
				String name = mUsernameEtxt.getText().toString().trim();
				String password = mPasswordEtxt.getText().toString().trim();
				String repeatPassword = mRepeatPasswordEtxt.getText()
						.toString().trim();
				String email = mEmailEtxt.getText().toString().trim();

				if (!TextUtils.isEmpty(name) && !TextUtils.isEmpty(password)
						&& !TextUtils.isEmpty(repeatPassword)
						&& password.equals(repeatPassword)) {
					MeetingReturned ret = mMeetingApi.register(name, password,
							email);
					if (ret != null) {
						switch (ret.operCode) {
						case MeetingReturned.CODE_OK:
							Account.my.name = name;
							Account.my.password = password;
							Account.my.roomId = ret.roomId;
							Account.my.roomPassword = "";

							mRefresher
									.sendEmptyMessage(REFRESH_SHOW_REG_DIALOG);
							break;
						default:
							mRefresher.sendEmptyMessage(ret.operCode);
							break;
						}
					} else {
						mRefresher
								.sendEmptyMessage(MeetingReturned.CODE_NO_RESPONSE);
					}
				} else {
					mRefresher.sendEmptyMessage(REFRESH_LOCAL_CHECK_HINT);
				}

			}
		}

	}

	private class Refresher extends Handler {
		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			mWaitDialog.cancel();
			String hint = null;
			switch (msg.what) {
			case REFRESH_SHOW_REG_DIALOG:
				showRegisterDialog();
				break;
			case REFRESH_LOCAL_CHECK_HINT:
				hint = "您的注册信息填写错误，请检查";
				break;
			case MeetingReturned.CODE_USER_ALREADY_EXISTED:
				hint = "您注册的用户名已经被使用，请更换再试";
				break;
			case MeetingReturned.CODE_USERNAME_INVALID:
				hint = "您的用户名不符合要求，请更换再试";
				break;
			case MeetingReturned.CODE_PASSWORD_INVALID:
				hint = "您的密码中不符合要求，请更换再试";
				break;
			case MeetingReturned.CODE_EMAIL_INVALID:
				hint = "您的邮件地址不符合要求，请更换再试";
				break;
			case MeetingReturned.CODE_NO_RESPONSE:
				hint = "服务器无响应，请稍后再试";
				break;
			}

			if (hint != null) {
				Toast toast = Toast.makeText(context, hint, Toast.LENGTH_SHORT);
				toast.setGravity(Gravity.CENTER, 0, 0);
				toast.show();
			}
		}
	}

}
